package cn.iocoder.yudao.module.train.service.coursecommodity;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.module.train.controller.admin.order.vo.OrderPageReqVO;
import cn.iocoder.yudao.module.train.dal.dataobject.connectDTO.CourseCommodityDTO;
import cn.iocoder.yudao.module.train.dal.mysql.order.OrderMapper;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.train.controller.admin.coursecommodity.vo.*;
import cn.iocoder.yudao.module.train.dal.dataobject.coursecommodity.CourseCommodityDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.train.dal.mysql.coursecommodity.CourseCommodityMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.yudao.module.train.enums.ErrorCodeConstants.*;

/**
 * 课程商品管理 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class CourseCommodityServiceImpl implements CourseCommodityService {

    @Resource
    private CourseCommodityMapper courseCommodityMapper;

    @Resource
    private OrderMapper orderMapper;

    @Override
    public Long createCourseCommodity(CourseCommoditySaveReqVO createReqVO) {
        // 插入
        CourseCommodityDO courseCommodity = BeanUtils.toBean(createReqVO, CourseCommodityDO.class);
        courseCommodityMapper.insert(courseCommodity);

        // 返回
        return courseCommodity.getId();
    }

    @Override
    public void updateCourseCommodity(CourseCommoditySaveReqVO updateReqVO) {
        // 校验存在
        validateCourseCommodityExists(updateReqVO.getId());
        // 更新
        CourseCommodityDO updateObj = BeanUtils.toBean(updateReqVO, CourseCommodityDO.class);
        courseCommodityMapper.updateById(updateObj);
    }

    @Override
    public void deleteCourseCommodity(Long id) {
        // 校验存在
        validateCourseCommodityExists(id);
        // 删除
        courseCommodityMapper.deleteById(id);
    }

    @Override
        public void deleteCourseCommodityListByIds(List<Long> ids) {
        // 删除
        courseCommodityMapper.deleteByIds(ids);
        }


    private void validateCourseCommodityExists(Long id) {
        if (courseCommodityMapper.selectById(id) == null) {
            throw exception(COURSE_COMMODITY_NOT_EXISTS);
        }
    }

    @Override
    public CourseCommodityDTO getCourseCommodity(Long id) {
        return courseCommodityMapper.selectCourseCommodity(id);
    }

    @Override
    public PageResult<CourseCommodityDTO> getCourseCommodityPage(CourseCommodityPageReqVO pageReqVO) {
        PageResult<CourseCommodityDTO> courseCommodityDTOPageResult = courseCommodityMapper.selectPage(pageReqVO);
        courseCommodityDTOPageResult.getList().forEach(courseCommodityDTO -> {
            OrderPageReqVO orderPageReqVO = new OrderPageReqVO();
            orderPageReqVO.setCommodityId(courseCommodityDTO.getId());
            Long count = (long) orderMapper.selectList(orderPageReqVO).size();
            courseCommodityDTO.setApplicantNumber(count);
            courseCommodityMapper.updateById(courseCommodityDTO);
        });
        return courseCommodityMapper.selectPage(pageReqVO);
    }

    @Override
    public List<CourseCommodityDTO> getCourseCommodityList(CourseCommodityPageReqVO pageReqVO) {
        PageResult<CourseCommodityDTO> courseCommodityDTOPageResult = courseCommodityMapper.selectPage(pageReqVO);
        courseCommodityDTOPageResult.getList().forEach(courseCommodityDTO -> {
            OrderPageReqVO orderPageReqVO = new OrderPageReqVO();
            orderPageReqVO.setCommodityId(courseCommodityDTO.getId());
            Long count = (long) orderMapper.selectList(orderPageReqVO).size();
            courseCommodityDTO.setApplicantNumber(count);
            courseCommodityMapper.updateById(courseCommodityDTO);
        });
        return courseCommodityMapper.selectList(pageReqVO);
    }

}
